<?php

namespace App\Helpers;

use Illuminate\Http\Response;

/**
 * Response represents an HTTP response.
 *
 * @author Paul Ventura <paul@yolotechnology.com>
 */
class ValidatorResponse {

    public static function handler($errorBag, $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY)
    {
        foreach ($errorBag->toArray() as $key => $value) {
            $errors[] = [
                'field' => $key,
                'message' => $value
            ];
        }

        return static::transformer($errors, $statusCode);
    }

    public static function genericHandler($errorBag, $statusCode)
    {
        foreach ($errorBag as $key => $value) {
            $errors[] = [
                'field' => null,
                'message' => [$value]
            ];
        }

        return static::transformer($errors, $statusCode);
    }

    public static function transformer($errors, $statusCode)
    {
        return response()->json([
            'message' => Response::$statusTexts[$statusCode],
            'data' => $errors,
            'status_code' => $statusCode
        ], $statusCode);
    }

}